﻿Imports SdlDotNet.Audio
Imports SdlDotNet.Graphics
Imports SdlDotNet.Graphics.Sprites
Imports System.Timers
Imports System.Drawing

Public Class Missile
    Implements ITemporaryDrawableEntity

    Private Position As Point

    Private LaunchLocation As Point
    Private TargetCity As City
    Private TargetPlayer As Player
    Private AmountOfDamageToDo As Integer
    Private WithEvents MovementTimer As New Timer(1)

    Private MissileSprite As New Sprite("resources\images\game\Missile.png")

    Private Status As MissileStatus = MissileStatus.GoingUp

    Public Event NoLongerInUse(ByRef sender As ITemporaryDrawableEntity) Implements ITemporaryDrawableEntity.NoLongerInUse
    Public Event Explosion(ByVal position As Point)
    Public Event MissileWasADud(ByRef sender As Missile)
    Public Event MissileDestroyedByAntiMissile(ByRef defendingPlayer As Player)

    Public Sub New(ByVal launchLocation As Point, ByRef targetPlayer As Player, ByRef targetCity As City, ByVal amountOfDamageToDo As Integer)
        Me.LaunchLocation = launchLocation
        Me.TargetCity = targetCity
        Me.TargetPlayer = targetPlayer
        Me.AmountOfDamageToDo = amountOfDamageToDo
        Position = launchLocation
        MovementTimer.Start()
        CommonSounds.Rocket.Play()
    End Sub

    Private Sub MovementTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MovementTimer.Elapsed
        If (Status = MissileStatus.GoingUp) Then
            Position.Y -= 2
            If (Position.Y < -30) Then
                Position.X = TargetCity.Position.X
                Status = MissileStatus.GoingDown
                MissileSprite.Surface = MissileSprite.Surface.CreateFlippedVerticalSurface()
            End If
        ElseIf (Status = MissileStatus.GoingDown) Then
            Position.Y += 2
            If (Position.Y > TargetCity.Position.Y) Then
                MissileReachedTarget()
            End If
        End If
    End Sub

    Private Sub MissileReachedTarget()
        Status = MissileStatus.DoneMoving

        If (TargetPlayer.AntiMissileDeployed) Then
            RaiseEvent MissileDestroyedByAntiMissile(TargetPlayer)
        ElseIf (CheckIfMissileIsDud() = False) Then
            TargetCity.Health -= AmountOfDamageToDo
            RaiseEvent Explosion(TargetCity.Position)
        End If

        RaiseEvent NoLongerInUse(Me)
    End Sub

    Public Sub Dispose() Implements ITemporaryDrawableEntity.Dispose
        MovementTimer.Dispose()
        MissileSprite.Dispose()
    End Sub

    Public Sub Draw(ByRef display As SdlDotNet.Graphics.Surface) Implements ITemporaryDrawableEntity.Draw
        If (Status = MissileStatus.GoingUp) Then
            display.Blit(MissileSprite, Position)
        ElseIf (Status = MissileStatus.GoingDown) Then
            display.Blit(MissileSprite, Position)
        End If
    End Sub

    Private Function CheckIfMissileIsDud() As Boolean
        If (RandomNumberProvider.GetRandomNumber(1, 100) <= 5) Then
            RaiseEvent MissileWasADud(Me)
            Return True
        Else
            Return False
        End If
    End Function

End Class